package cn.bonoon.kernel.web.models;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.servlet.ModelAndView;

import cn.bonoon.kernel.util.StringHelper;

/**
 * <pre>
 * 各种界面容器的model父类
 * </pre>
 * @author jackson
 *
 */
public class StandardModel extends ModelAndView {
	
	private final static String DEFAULT_LAYOUT_KEY = "layout";
	private String layoutKey = DEFAULT_LAYOUT_KEY;
	
	/** 需要被引入的js/css文件和html源码等 */
	protected StringBuilder includes;
	/** 定义在jquery.load函数里被执行的代码块 */
	protected final StringBuilder block = new StringBuilder();
	/** 直接的js代码块 */
	protected StringBuilder script;

	protected String url;
	/** 菜单的ID，并不是每一个页面都有唯一的菜单ID */
	protected String mid;
	
	protected final HttpServletRequest request;
	protected String master;
	protected String modelName = "model";
	
	protected String sysWarnMessage;

	public String getSysWarnMessage() {
		return sysWarnMessage;
	}

	public void setSysWarnMessage(String sysWarnMessage) {
		this.sysWarnMessage = sysWarnMessage;
	}

	public StandardModel(String mid, HttpServletRequest request) {
		this.mid = mid;
		this.request = request;
		// url的参数
		this.url = request.getRequestURI();
	}
	
	public String getMid() {
		return mid;
	}

	public StandardModel setMaster(String master) {
		if (StringHelper.isNotEmpty(master)){
			this.master = master;
		}
		return this;
	}

	public StandardModel setLayoutKey(String layoutKey) {
		this.layoutKey = layoutKey;
		return this;
	}

	public void addIncludes(StringBuilder includes) {
		this.includes = merger(this.includes, includes);
	}

	public void addBlock(String js) {
		block.append(js);
	}

	public void addBlock(StringBuilder js) {
		if(null != js){
			block.append(js);
		}
	}

	public void addFun(String name, String body) {
		if(null == script){
			script = new StringBuilder();
		}
		script.append("function ").append(name).append("(){").append(body).append('}');
	}

	public void addScript(StringBuilder script) {
		this.script = merger(this.script, script);
	}
	
	public StringBuilder getBlock() {
		return block;
	}

	public StringBuilder getScript() {
		return script;
	}
	
	public StringBuilder getIncludes() {
		return includes;
	}
	
	protected StringBuilder merger(StringBuilder source, StringBuilder target){
		if(null != target && target.length() > 0){
			if(null == source){
				source = new StringBuilder();
			}
			source.append(target);
		}
		return source;
	}

	public StandardModel execute(String view) {
		setViewName(view);
		addObject(modelName, this);
		addObject(layoutKey, master);
		return this;
	}

	public StandardModel setModelName(String modelName) {
		this.modelName = modelName;
		return this;
	}

	/**
	 * <pre>
	 * 当前访问的URL,如果该应用服务不是放在根目录下的,在该路径的前面会带有ContextPath.
	 * 即当前的URL是可以直接通过浏览器来访问的路径.
	 * 
	 * 注意与方法<code>String getDefinedUrl()</code>的区别
	 * </pre>
	 * 
	 * @return
	 */
	public String getUrl() {
		return url;
	}

	public int intParameter(String name) {
		return StringHelper.toint(request.getParameter(name));
	}
	
	public long longParameter(String name){
		return StringHelper.tolong(request.getParameter(name));
	}

	public Long getLong(String name){
		return StringHelper.toLong(request.getParameter(name));
	}
	
	public String getParameter(String name) {
		return request.getParameter(name);
	}

	public String[] getParameters(String name) {
		return request.getParameterValues(name);
	}
	
//	/**
//	 * 把路径加上系统所在的工作区的路径，即<code>request.getContextPath() + url</code>
//	 * @param url
//	 * @return
//	 */
//	public String path(String url){
//		return StringHelper.path(request.getContextPath(), url);
//	}
}
